package com.gengzi.lot.item.controller;

import com.gengzi.lot.common.pojo.PageResult;
import com.gengzi.lot.item.pojo.Lot;
import com.gengzi.lot.item.service.LotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("lot")
@Api(value = "LotController")
public class LotController {
    @Autowired
    private LotService lotService;

    /**
     * 根据停车场id查询停车位list
     * @param parkId
     * @return
     */
    @GetMapping("page")
    @ApiOperation(notes = "查询停车位", value = "queryLots")
    public ResponseEntity<List<Lot>> queryLots() {
        List<Lot> lots = this.lotService.queryLots();
        if (CollectionUtils.isEmpty(lots)) {
            // 响应404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(lots);
    }
    @GetMapping("list")
    @ApiOperation(notes = "查询停车位", value = "queryLots")
    public ResponseEntity<PageResult<Lot>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc,
            @RequestParam(value = "key", required = false) String key) {
        PageResult<Lot> result = this.lotService.queryLot(page,rows,sortBy,desc, key);
        if (result == null || result.getItems().size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        result.setTotalPage(page.longValue());
        return ResponseEntity.ok(result);
    }

    /**
     * 根据停车场id查询停车位list
     * @param parkId
     * @return
     */
    @GetMapping("lots")
    @ApiOperation(notes = "根据停车场id查询停车位", value = "queryLotsByParkId")
    public ResponseEntity<List<Lot>> queryLotsByParkId(@RequestParam("parkId") Long parkId) {

        if (parkId == null || parkId.longValue() < 0) {
            // 响应400，相当于ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            return ResponseEntity.badRequest().build();
        }
        List<Lot> lots = this.lotService.queryLotsByParkId(parkId);
        if (CollectionUtils.isEmpty(lots)) {
            // 响应404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(lots);
    }

    /**
     * 根据停车位id查询停车位
     * @param lotId
     * @return
     */
    @GetMapping("lot")
    @ApiOperation(notes = "根据停车位id查询停车位", value = "queryLotByLotId")
    public ResponseEntity<Lot> queryLotByLotId(@RequestParam("lotId") Long lotId,@RequestParam("ParkId") Long parkId) {

        if (lotId == null || lotId.longValue() < 0) {
            // 响应400，相当于ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            return ResponseEntity.badRequest().build();
        }
        Lot lot = this.lotService.queryLotByLotId(lotId,parkId);
        if (lot == null) {
            // 响应404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(lot);
    }

    @PostMapping("lot")
    @ApiOperation(notes = "添加停车位", value = "addLot")
    public ResponseEntity<Void> addLot(Lot lot) {
        // TODO 打桩
        System.out.println(lot);
        if (lot.getParkId() != null) {
            Boolean boo = this.lotService.addLot(lot);
            if (boo == null || !boo) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        }
        // TODO 打桩
        System.out.println("添加成功！");
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("lot")
    @ApiOperation(notes = "修改停车位", value = "updateLot")
    public ResponseEntity<Void> uplodLot(Lot lot) {
        try {
            this.lotService.update(lot);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
